package cn.it.xiaohan.note.web;

import cn.hutool.core.util.StrUtil;
import cn.it.xiaohan.note.po.Note;
import cn.it.xiaohan.note.po.NoteType;
import cn.it.xiaohan.note.po.User;
import cn.it.xiaohan.note.service.NoteService;
import cn.it.xiaohan.note.service.NoteTypeService;
import cn.it.xiaohan.note.vo.ResultInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/note")
public class NoteServlet extends HttpServlet {

    private NoteService noteService = new NoteService();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置导航栏高亮
        request.setAttribute("menu_page","note");

        // 接收用户行为
        String actionName = request.getParameter("actionName");

        if ("view".equals(actionName)){

            // 进入发表云记首页
            noteView(request,response);
        } else if ("addOrUpdate".equals(actionName)){

            // 添加或者修改云记
            addOrUpdateNote(request,response);
        } else if ("detail".equals(actionName)){

            // 查询云记详情
            noteDetail(request,response);
        } else if ("delete".equals(actionName)){

            // 删除云记操作
            deleteNote(request,response);
        }
    }

    /**
     * 删除云记
     * @param request
     * @param response
     */
    private void deleteNote(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 接收参数
        String noteId = request.getParameter("noteId");
        // 调用Service层的删除方法返回状态码
        Integer code = noteService.deleteNote(noteId);
        // 通过流的方式响应给前台的Ajax回调函数
        PrintWriter out = response.getWriter();
        out.write(code + "");
        out.close();
    }

    /**
     * 查询云记详情
     * @param request
     * @param response
     */
    private void noteDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1、接收参数
        String noteId = request.getParameter("noteId");
        // 2、调用Service层的查询方法，返回Note对象
        Note note = noteService.findNoteById(noteId);
        // 3、将note对象存放到request作用域中
        request.setAttribute("note",note);
        // 4、设置动态包含的页面值
        request.setAttribute("changePage","note/detail.jsp");
        // 5、请求转发到index.jsp
        request.getRequestDispatcher("index.jsp").forward(request,response);
    }

    /**
     * 添加或者修改云记
     * @param request
     * @param response
     */
    private void addOrUpdateNote(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        // 获取参数
        String typeId = request.getParameter("typeId");
        String title = request.getParameter("title");
        String content = request.getParameter("content");

        // 获取经纬度
        String lon = request.getParameter("lon");
        String lat = request.getParameter("lat");

        // 如果是修改操作，需要接收noteId
        String noteId = request.getParameter("noteId");

        if (lon == null){
            lon = "115.869";
        }
        // 调用Service层返回resultInfo对象
        ResultInfo<Note> resultInfo = noteService.addOrUpdate(typeId,title,content,noteId,lon,lat);

        // 判断成功还是失败
        if (resultInfo.getCode() == 1){
            // 如果成功跳转到到首页
            response.sendRedirect("index");
        }else {
            // 如果失败返回到当前页面
            request.setAttribute("resultInfo",resultInfo);
            // 请求转发到note?actionName=view
            String url = "note?actionName=view";
            // 如果是修改操作，需要传递noteId
            if (!StrUtil.isBlank(noteId)){
                url += "&noteId=" + noteId;
            }
            request.getRequestDispatcher(url).forward(request,response);
        }
    }

    /**
     * 进入发布云记页面
        1.从session作用域中获取对象
​		2、通过id查询对应的类型列表
​		3、将类型设置到request作用域中
​		4、设置动态包含的页面值
​		5、请求转发跳转到index.jsp
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void noteView(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*修改云记*/
        // 得到想要修改的Id
        String noteId = request.getParameter("noteId");
        // 通过noteID查询云记对象
        Note note = noteService.findNoteById(noteId);
        // 将的到的note对象存放到request作用域中
        request.setAttribute("noteInfo",note);
        /*修改云记*/

        // 从session作用域中获取对象
        User user = (User) request.getSession().getAttribute("user");
        // 通过id查询对应的类型列表
        List<NoteType> typeList = new NoteTypeService().findTypeByUserId(user.getUserId());
        // 将类型设置到request作用域中
        request.setAttribute("typeList",typeList);
        // 4.设置动态包含页面值
        request.setAttribute("changePage","note/view.jsp");
        // 5.请求转发跳转到index.jsp
        request.getRequestDispatcher("index.jsp").forward(request,response);
    }
}
